package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.JsonObject;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * 体检套餐服务
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;

    //新增套餐信息，同时需要关联检查组
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckgroup(setmealId, checkgroupIds);
        //将图片名称保存到Redis集合中
        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, fileName);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> page = setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    public List<Setmeal> findAll() {
        //1.判断redis集合中是否存在当前套餐列表
        //从redis中查询数据,使用sortedest排序查询
        String setmealList1 = jedisPool.getResource().get("setmealList");
        if (setmealList1 != null && setmealList1.length() > 0) {
            //套餐列表在当前redis集合中,直接从redis中查询
            System.out.println("在redis中查询");
            List<Setmeal> parse = (List<Setmeal>)JSONObject.parse(setmealList1);
            return parse;
        }
        //套餐列表不在当前redis集合中,从数据库查询,查询到后保存到redis集合中
        System.out.println("数据库");
        //从数据库查询
        List<Setmeal> setmealList = setmealDao.findAll();
        //将setList保存到redis中
        String s = JSONObject.toJSONString(setmealList);
        jedisPool.getResource().set("setmealList",s);
        return setmealList;
    }

    //根据套餐ID查询套餐详情（套餐基本信息、套餐对应的检查组信息、检查组对应的检查项信息）
    public Setmeal findById(int id) {
        String setmeal1 = jedisPool.getResource().get("setmeal");
        if (setmeal1 != null && setmeal1.length()>0){
            //redis中有数据,直接从redis中查询
            System.out.println("redis:详情");
            Setmeal parse = (Setmeal)JSON.parseObject(setmeal1,Setmeal.class);
            return parse;
        }else {
            //redis中没有数据,从数据陆查询
            System.out.println("数据库详情");
            Setmeal setmeal = setmealDao.findById(id);
            //将查询到的数据储存到redis中
            String s = JSONObject.toJSONString(setmeal);
            jedisPool.getResource().set("setmeal",s);
            return setmeal;
        }



    }

    //查询套餐预约占比数据
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }

    @Override
    public List<Integer> findBySetMealId(Integer id) {
        List<Integer> setmeals = setmealDao.findBySetMealId(id);
        return setmeals;
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.edit(setmeal);
        Integer setmealId = setmeal.getId();
        //根据id删除关联关系
        setmealDao.delete(setmealId);
        setSetmealAndCheckgroup(setmealId, checkgroupIds);
        //修改检查套餐


    }

    //设置套餐和检查组多对多关系，操作t_setmeal_checkgroup
    public void setSetmealAndCheckgroup(Integer setmealId, Integer[] checkgroupIds) {
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmealId", setmealId);
                map.put("checkgroupId", checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }
}
